package com.region.integral.common_user.obtain.dao.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import org.springframework.stereotype.Component;

import com.region.integral.common_user.obtain.dao.IIntegralLogin;
import com.region.integral.common_user.obtain.vo.IntegralData;
import com.region.integral.common_user.pojo.CUIEntity;
import com.region.integral.common_user.xml.Regulation;
import com.region.integral.common_user.xml.dto.Login;
import com.region.integral.exception.IntegralException;
import com.region.integral.hibernate.BaseHibernate;
import com.region.integral.util.CommonUserLogUtil;
import com.region.integral.util.XMLRegulationUtil;
@Component("iLogin")
public class IntegralLoginImpl extends BaseHibernate implements IIntegralLogin {

	@Override
	public void login(int userid,IntegralData integralData)throws IntegralException {
		CUIEntity cuiEntity = getCUIEntity(userid);
		if (cuiEntity != null) {
			int loginTimes = cuiEntity.getLoginTimes();
			Date lastLoginDate = cuiEntity.getLastLoginDate();
			Date now = new Date();
			if(lastLoginDate != null){
				Calendar lastDate = new GregorianCalendar();
				lastDate.setTime(lastLoginDate);
				lastDate.add(Calendar.DAY_OF_MONTH, 1);
				Calendar nowDate = new GregorianCalendar();
				nowDate.setTime(now);
				if(nowDate.equals(lastDate))
				{
					loginTimes = loginTimes + 1;
				}
				else{
					loginTimes = 0;
				}
			}
			
			Regulation regulation = XMLRegulationUtil.unmarshaller();
			List<Login> logins = regulation.getLogin();
			for (Login login : logins) {
				if (login.getBelow()!=0 && login.getBetween()==0&& login.getBelow() >= loginTimes) {
					int currIntegral = cuiEntity.getCurrIntegral();
					int maxIntegral = cuiEntity.getMaxIntegral();
					int totalIntegral = currIntegral+ login.getIntegral();
					cuiEntity.setCurrIntegral(totalIntegral);
					if (maxIntegral < totalIntegral) {
						cuiEntity.setMaxIntegral(totalIntegral);
					}
					cuiEntity.setLastLoginDate(now);
					cuiEntity.setLoginTimes(loginTimes);
					integralData.setSuccee(true);
					hibernateTemplate.update(cuiEntity);
					hibernateTemplate.save(CommonUserLogUtil.log(userid, login.getIntegral(), "login"));
				}
				if (login.getBetween()!=0 &&login.getBelow()< loginTimes && login.getBetween() >= loginTimes) {
					int currIntegral = cuiEntity.getCurrIntegral();
					int maxIntegral = cuiEntity.getMaxIntegral();
					int totalIntegral = currIntegral+ login.getIntegral();
					cuiEntity.setCurrIntegral(totalIntegral);
					if (maxIntegral < totalIntegral) {
						cuiEntity.setMaxIntegral(totalIntegral);
					}
					cuiEntity.setLastLoginDate(now);
					cuiEntity.setLoginTimes(loginTimes);
					integralData.setSuccee(true);
					hibernateTemplate.update(cuiEntity);
					hibernateTemplate.save(CommonUserLogUtil.log(userid, login.getIntegral(), "login"));
				}
				if (login.getOver()!=0 && login.getOver() < loginTimes) {
					int currIntegral = cuiEntity.getCurrIntegral();
					int maxIntegral = cuiEntity.getMaxIntegral();
					int totalIntegral = currIntegral+ login.getIntegral();
					cuiEntity.setCurrIntegral(totalIntegral);
					if (maxIntegral < totalIntegral) {
						cuiEntity.setMaxIntegral(totalIntegral);
					}
					cuiEntity.setLastLoginDate(now);
					cuiEntity.setLoginTimes(loginTimes);
					integralData.setSuccee(true);
					hibernateTemplate.update(cuiEntity);
					hibernateTemplate.save(CommonUserLogUtil.log(userid, login.getIntegral(), "login"));
				}
			}
		}
	}

}
